/**
 * 登录验证
 * 2018-01-11 16:12:37
 */
function ulogin() {
    console.log('this function is called')
    console.log(username)
    // 登录之前 先调用验证函数
    // 验证用户名和密码
    var username = document.getElementById('username').value
    var password = document.getElementById('password').value
    if (validate()) {
        api.login({ username: username, password: password })
            .then(login => {
                // 这里的login为ajax返回的信息
                // 这里修改ajax请求默认配置，在请求头中默认添加Authorization，做token权限认证
                // api.ajax.defaults.headers.common['Authorization'] = login.result.token
                if (
                    (api.ajax.defaults.headers.common['Authorization'] =
                        login.result.token)
                )
                    Cookies.set('token', login.result.token)
                location.href = './index.html'
            })
    }
}

/**
 * 登录框信息录入验证
 * @return boolean
 * 2018-01-11 16:15:36
 */
function validate() {
    var msg
    if (username == '') {
        msg = '用户名不能为空'
        console.log(msg)
        validateError('username', msg)

        // return false;
    }

    if (password == '') {
        msg = '密码不能为空'
        console.log(msg)
        validateError('password', msg)
    }

    if (username && password) {
        return true
    }
}

/**
 * 验证错误信息提示
 */
function validateError(validNode, msg) {
    console.log(document.getElementById(validNode))
    var errorNode = document.getElementById(validNode)
    // var node = document.createTextNode("<span>"+msg+"</span>");
    // console.log(node);
    console.log(validNode)
    errorNode.value = msg
    // errorNode.appendChild(node);

    return false
}
